package com.interceptor;

import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;

import java.lang.reflect.Method;
import java.util.concurrent.Callable;

public class TimingInterceptor {
    @RuntimeType
    public static Object intercept(@Origin Method method,
                                   @SuperCall Callable<?> caller) throws Exception {
        long s = System.nanoTime();
        try { return caller.call(); }
        finally { System.out.println(method + " took " + (System.nanoTime()-s)/1_000_000+" ms"); }
    }
}